package com.guigu.servicegoods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.servicegoods.domain.TbPara;
import com.guigu.servicegoods.mapper.TbParaMapper;
import com.guigu.servicegoods.service.TbParaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbParaServiceImpl extends ServiceImpl<TbParaMapper, TbPara> implements TbParaService {

    @Override
    public Result findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        QueryWrapper<TbPara> wrapper = createExample(searchMap);
        Page<TbPara> paraPage = new Page<>(page,size);
        baseMapper.selectPage(paraPage,wrapper);
        return Result.ok().data("row",paraPage.getRecords()).data("total",paraPage.getTotal());
    }

    // 根据查询条件封装模块对象
    private QueryWrapper<TbPara> createExample(Map<String, Object> searchMap) {
        QueryWrapper<TbPara> wrapper = new QueryWrapper<>();
        if(searchMap!=null) {
            if(searchMap.get("id")!=null)
                wrapper.eq("id",searchMap.get("id"));
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name")))
                wrapper.like("name",searchMap.get("name"));
            if(searchMap.get("templateId")!=null)
                wrapper.eq("template_id",searchMap.get("templateId"));
        }
        return wrapper;
    }
}
